package net.lab1024.sa.admin.module.business.project.service;

import java.time.LocalDateTime;
import java.util.List;
import net.lab1024.sa.admin.module.business.project.dao.ProjectInfoDao;
import net.lab1024.sa.admin.module.business.project.domain.entity.ProjectInfoEntity;
import net.lab1024.sa.admin.module.business.project.domain.form.ProjectInfoAddForm;
import net.lab1024.sa.admin.module.business.project.domain.form.ProjectInfoQueryForm;
import net.lab1024.sa.admin.module.business.project.domain.form.ProjectInfoUpdateForm;
import net.lab1024.sa.admin.module.business.project.domain.vo.ProjectInfoVO;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.module.support.datatracer.constant.DataTracerTypeEnum;
import net.lab1024.sa.base.module.support.datatracer.service.DataTracerService;
import net.lab1024.sa.base.module.support.serialnumber.constant.SerialNumberIdEnum;
import net.lab1024.sa.base.module.support.serialnumber.service.SerialNumberService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

/**
 * 项目表 Service
 *
 * @Author zzx
 * @Date 2025-08-07 13:47:01
 * @Copyright zzx
 */

@Service
public class ProjectInfoService {

    @Resource
    private ProjectInfoDao projectInfoDao;
    @Resource
    private DataTracerService dataTracerService;
    @Resource
    private SerialNumberService serialNumberService;

    /**
     * 分页查询
     */
    public PageResult<ProjectInfoVO> queryPage(ProjectInfoQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ProjectInfoVO> list = projectInfoDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(ProjectInfoAddForm addForm) {
        ProjectInfoEntity projectInfoEntity = SmartBeanUtil.copy(addForm, ProjectInfoEntity.class);
        RequestUser user = SmartRequestUtil.getRequestUser();
        projectInfoEntity.setCreateUserId(user.getUserId());
        projectInfoEntity.setProjectDateId(serialNumberService.generate(SerialNumberIdEnum.ORDER));
        projectInfoDao.insert(projectInfoEntity);
        dataTracerService.insert(projectInfoEntity.getProjectId(), DataTracerTypeEnum.PROJECT);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(ProjectInfoUpdateForm updateForm) {
        ProjectInfoEntity projectInfoEntity = SmartBeanUtil.copy(updateForm, ProjectInfoEntity.class);
        projectInfoDao.updateById(projectInfoEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        projectInfoDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long projectId) {
        if (null == projectId){
            return ResponseDTO.ok();
        }
        projectInfoDao.updateDeleted(projectId, true);
        return ResponseDTO.ok();
    }

    /**
     * 项目详情
     * @param projectId
     * @return
     */
    public ProjectInfoVO getDetail(Long projectId) {
        return projectInfoDao.getDetail(projectId, false);
    }
}
